﻿Imports Data


Public Class TipoProducto
#Region "Variables estaticas"
    Private id_tipoProducto As Integer

#End Region

#Region "Enum"
    Private Enum Tipos_Modiciacion_Interfaz
        Ingreso = 1
        Modificacion = 2
        Eliminacion = 3
    End Enum

#End Region

    Private Sub TipoProducto_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            Me.CargarListView()
        Catch ex As Exception

        End Try
    End Sub

#Region "Metodos de Cargas"

    Private Sub CargarListView()
        Try
            Me.lv_tipoProducto.Items.Clear()
            Using db As New SerigraphicaDBDataContext
                Dim l_tipoProducto As New List(Of Tipo_Producto)
                l_tipoProducto = db.Tipo_Producto.Select(Function(p) p).ToList

                lv_tipoProducto.BeginUpdate()
                For Each item In l_tipoProducto
                    lv_tipoProducto.Items.Add(New ListViewItem(New String() {item.id, item.descripcion}) With {.Name = item.id})
                Next

                lv_tipoProducto.EndUpdate()
                lv_tipoProducto.Refresh()
            End Using
        Catch ex As Exception

        End Try
    End Sub

#End Region

#Region "Metodos CRUD"
    Private Function Ingresar(ByVal nombre As String) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim nuevoTipo As New Tipo_Producto
                nuevoTipo.descripcion = nombre
                db.Tipo_Producto.InsertOnSubmit(nuevoTipo)
                db.SubmitChanges()
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Function Modificar(ByVal descripcion As String) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim tipoProducto_ant As Tipo_Producto = db.Tipo_Producto.Select(Function(p) p).Where(Function(p) p.id = id_tipoProducto).FirstOrDefault
                tipoProducto_ant.descripcion = descripcion
                db.SubmitChanges()
                Me.btnIngresar.Text = "Ingresar"
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Function EliminarRegistro(ByVal id As Integer) As Boolean
        Using db As New SerigraphicaDBDataContext
            Try
                db.Connection.Open()
                db.Transaction = db.Connection.BeginTransaction(IsolationLevel.Serializable)

                Dim resultado = db.Tipo_Producto.FirstOrDefault(Function(p) p.id = id)
                db.Tipo_Producto.DeleteOnSubmit(resultado)

                db.SubmitChanges()
                db.Transaction.Commit()
                Return True
            Catch ex As Exception
                Debug.WriteLine(ex.Message, System.Reflection.MethodBase.GetCurrentMethod().Name)
                db.Transaction.Rollback()
                Return False
            Finally
                db.Connection.Close()
            End Try
        End Using
    End Function

#End Region

#Region "Metodos ToolStrip"

    Private Sub IngresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles IngresarToolStripMenuItem.Click
        Try
            Me.EditarInterfaz(Nothing, Tipos_Modiciacion_Interfaz.Ingreso)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub EliminarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Try
            If Me.lv_tipoProducto.SelectedItems.Count = 1 Then
                Dim id As Integer = CInt(Me.lv_tipoProducto.SelectedItems(0).Name)
                If MsgBox("Desea Elimnar este registro", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
                    Me.EliminarRegistro(id)
                    MsgBox("Mensaje eliminado", MsgBoxStyle.Information)
                    Me.CargarListView()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub EditarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EditarToolStripMenuItem.Click
        Try
            Dim id As Integer = CInt(lv_tipoProducto.SelectedItems(0).Name)
            Me.EditarInterfaz(id, Tipos_Modiciacion_Interfaz.Modificacion)
        Catch ex As Exception

        End Try
    End Sub

#End Region

#Region "Edicion/Limpieza Interfaz"
    Private Function EditarInterfaz(ByVal tipo As Integer, tipo_modificacion As Integer) As Boolean
        Using db As New SerigraphicaDBDataContext
            Try
                If tipo_modificacion = Tipos_Modiciacion_Interfaz.Ingreso Then
                    Me.txtNombre.Text = String.Empty
                    Me.btnIngresar.Text = "Ingresar"
                    Me.id_tipoProducto = Nothing
                ElseIf tipo_modificacion = Tipos_Modiciacion_Interfaz.Modificacion Then
                    Dim registro = db.Tipo_Producto.Select(Function(p) p).Where(Function(p) p.id = tipo).FirstOrDefault
                    Me.id_tipoProducto = registro.id
                    Me.txtNombre.Text = registro.descripcion
                    Me.btnIngresar.Text = "Modificar"
                End If
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Using
    End Function

    Private Sub LimpiarInterfaz()
        Try
            Me.txtNombre.Text = String.Empty
        Catch ex As Exception

        End Try
    End Sub
#End Region

#Region "Acciones: Click Boton"
    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Try
            Me.Close()

        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnIngresar_Click(sender As System.Object, e As System.EventArgs) Handles btnIngresar.Click
        Try
            If (String.Compare(Me.btnIngresar.Text, "Ingresar") = 0) Then
                Me.Ingresar(Me.txtNombre.Text)
                Me.CargarListView()
                Me.LimpiarInterfaz()
            Else
                Me.Modificar(Me.txtNombre.Text)
                Me.CargarListView()
                Me.LimpiarInterfaz()
            End If
        Catch ex As Exception

        End Try
    End Sub

#End Region

#Region "Accion: TextChanged"

    Private Sub txtNombre_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtNombre.TextChanged
        If (String.IsNullOrEmpty(txtNombre.Text)) Then
            Me.btnIngresar.Enabled = False
        Else
            Me.btnIngresar.Enabled = True
        End If
    End Sub

#End Region


End Class